package com.skivingcloud.workflow.service.impl;

import com.skivingcloud.admin.sys.entity.User;
import com.skivingcloud.common.utils.PageUtils;
import com.skivingcloud.common.utils.StringUtils;
import com.skivingcloud.common.utils.UuidUtil;
import com.skivingcloud.workflow.entity.Strategy;
import com.skivingcloud.workflow.mapper.StrategyMapper;
import com.skivingcloud.workflow.service.IStrategyService;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import org.springframework.stereotype.Service;

import net.sf.json.JSONObject;

/**
 * <p>
 * 找人策略 服务实现类
 * </p>
 *
 * @author skivingcloud
 * @since 2022-03-03
 */
@Service
public class StrategyServiceImpl extends ServiceImpl<StrategyMapper, Strategy> implements IStrategyService {

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		PageUtils pageUtils = PageUtils.getPageInfo(params);
		Page<Map<String, Object>> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
		Strategy strategy = (Strategy) JSONObject.toBean(JSONObject.fromObject(params.get("dataForm")), Strategy.class);
		if (StringUtils.isBlank(strategy.getStrategyName())) {
			strategy.setStrategyName(null);
		} else {
			strategy.setStrategyName("%" + strategy.getStrategyName().trim() + "%");
		}
		if (StringUtils.isBlank(strategy.getStrategyCode())) {
			strategy.setStrategyCode(null);
		} else {
			strategy.setStrategyCode("%" + strategy.getStrategyCode().trim() + "%");
		}
		if (StringUtils.isBlank(strategy.getStrategyType())) {
			strategy.setStrategyType(null);
		}
		List<Map<String, Object>> list = this.baseMapper.selectByPram(strategy);
		pageUtils = new PageUtils(p);
		return pageUtils;
	}

	@Override
	public void save(Strategy strategy, User user) {
		LocalDateTime date = LocalDateTime.now();
		strategy.setId(UuidUtil.get32UUID());
		strategy.setCreater(user.getId());
		strategy.setCreateTime(date);
		strategy.setModUser(user.getId());
		strategy.setLastModTime(date);
		this.baseMapper.insert(strategy);
	}

	@Override
	public Strategy selectById(String id) {
		return this.baseMapper.selectById(id);
	}

	@Override
	public void update(Strategy strategy, User user) {
		strategy.setModUser(user.getId());
		strategy.setLastModTime(LocalDateTime.now());
		this.baseMapper.updateById(strategy);
	}

	@Override
	public void deleteBatch(String[] ids) {
		this.baseMapper.deleteBatchIds(Arrays.asList(ids));
	}

	@Override
	public List<Strategy> select(Strategy strategy) {
		QueryWrapper<Strategy> queryWrapper = new QueryWrapper<Strategy>();
		return this.baseMapper.selectList(queryWrapper);
	}

	@Override
	public Strategy getStrategyIdByCode(String strategyCode) {
		QueryWrapper<Strategy> queryWrapper = new QueryWrapper<Strategy>();
		queryWrapper.eq("strategy_code", strategyCode);
		List<Strategy> list = this.baseMapper.selectList(queryWrapper);
		return list.isEmpty() ? null : list.get(0);
	}
}
